##Title:Appendix 3-5
##Date: 18 Sept 2023

```{r preparation}
library(tidyverse)

library(plm)
library(modelsummary)

dat <- read.csv("D:/Essex_PhD/3rd_year/Data for R&R/Replication_R1_20240918/replication_20231015.csv") 


```


```{r Appendix 3 TableA.3}
#Table A.3: Extra Models used for sensitivity analysis 
dat$pko_aid <-(dat$treat_aid)*(dat$treat_pko)
#all sample


#FE without pko_aid
sens_fe.2 <- plm(osv_binary_forward1 ~ treat_aid + treat_pko  + 
                                   lnpop + prec_gpcp  + conflict_intensity + 
                                   spatial_lag_osv + spatial_lag_pko + 
                                    task_tamm + humaid_assist_tamm,
                     data = dat,
                         index = c("gid", "year"), model = "within")


#reb sample

#FE without pko_aid
reb_sens_fe.2 <- plm(reb_osv_binary_forward1 ~ treat_aid + treat_pko + 
                                   lnpop + prec_gpcp  + conflict_intensity + 
                                   spatial_lag_osv + spatial_lag_pko + 
                                   task_tamm + humaid_assist_tamm,   data = dat,
                    index = c("gid", "year"), model = "within")

#gov sample

#FE without pko_aid
gov_sens_fe.2 <- plm(gov_osv_binary_forward1 ~ treat_aid + treat_pko + 
                      lnpop + prec_gpcp  + conflict_intensity + 
                      spatial_lag_osv + spatial_lag_pko + 
                      task_tamm + humaid_assist_tamm,   data = dat,
                     index = c("gid", "year"), model = "within")


modelsummary(list(sens_fe.2, reb_sens_fe.2, gov_sens_fe.2), stars = TRUE)

#modelsummary(list(sens_fe.2, reb_sens_fe.2, gov_sens_fe.2), stars = TRUE, output = "-TableA.3.docx")
```

```{r Appendix 4 }
 
dat$pko_n_100 <- (dat$troop.no)/100
dat$aid_million_recode  <- (dat$sum_aid)/1000000

##DV as log_OSV(t+1)
##IV as  pko_n_100 + aid_million_recode

#all sample

dv_c_iv_c.1 <- plm(log_osv_forward1 ~ pko_n_100 + aid_million_recode + 
                     pko_n_100*aid_million_recode, 
                         data = dat,
                         index = c("gid", "year"), model = "within")

dv_c_iv_c.2 <- plm(log_osv_forward1 ~ pko_n_100 + aid_million_recode + 
                     pko_n_100*aid_million_recode +
                     lnpop + prec_gpcp + conflict_intensity + 
                     spatial_lag_osv + spatial_lag_pko  +
                     task_tamm + humaid_assist_tamm,
                     data = dat,
                         index = c("gid", "year"), model = "within")

#reb sample
reb_dv_c_iv_c.1 <- plm(log_reb_osv_forward1 ~ pko_n_100 + aid_million_recode + 
                     pko_n_100*aid_million_recode, 
                         data = dat,
                         index = c("gid", "year"), model = "within")

reb_dv_c_iv_c.2 <- plm(log_reb_osv_forward1 ~ pko_n_100 + aid_million_recode + 
                     pko_n_100*aid_million_recode +
                     lnpop + prec_gpcp + conflict_intensity + 
                     spatial_lag_osv + spatial_lag_pko  +
                     task_tamm + humaid_assist_tamm,
                     data = dat,
                         index = c("gid", "year"), model = "within")

#gov sample
gov_dv_c_iv_c.1 <- plm(log_gov_osv_forward1 ~ pko_n_100 + aid_million_recode + 
                     pko_n_100*aid_million_recode, 
                         data = dat,
                         index = c("gid", "year"), model = "within")

gov_dv_c_iv_c.2 <- plm(log_gov_osv_forward1 ~ pko_n_100 + aid_million_recode + 
                     pko_n_100*aid_million_recode +
                     lnpop + prec_gpcp + conflict_intensity + 
                     spatial_lag_osv + spatial_lag_pko  +
                     task_tamm + humaid_assist_tamm,
                     data = dat,
                         index = c("gid", "year"), model = "within")

##DV as OSV(t+1) Binary

##all sample
dv_b_iv_c.1 <- plm(osv_binary_forward1 ~ pko_n_100 + aid_million_recode + 
                     pko_n_100*aid_million_recode, 
                         data = dat,
                         index = c("gid", "year"), model = "within")

dv_b_iv_c.2 <- plm(osv_binary_forward1 ~ pko_n_100 + aid_million_recode + 
                     pko_n_100*aid_million_recode +
                     lnpop + prec_gpcp + conflict_intensity + 
                     spatial_lag_osv + spatial_lag_pko+ 
                     task_tamm + humaid_assist_tamm,
                     data = dat,
                         index = c("gid", "year"), model = "within")

#reb sample
reb_dv_b_iv_c.1 <- plm(reb_osv_binary_forward1 ~ pko_n_100 + aid_million_recode + 
                     pko_n_100*aid_million_recode, 
                         data = dat,
                         index = c("gid", "year"), model = "within")

reb_dv_b_iv_c.2 <- plm(reb_osv_binary_forward1 ~ pko_n_100 + aid_million_recode + 
                     pko_n_100*aid_million_recode +
                     lnpop + prec_gpcp + conflict_intensity + 
                     spatial_lag_osv + spatial_lag_pko+ 
                     task_tamm + humaid_assist_tamm,
                     data = dat,
                         index = c("gid", "year"), model = "within")

#gov sample
gov_dv_b_iv_c.1 <- plm(gov_osv_binary_forward1 ~ pko_n_100 + aid_million_recode + 
                     pko_n_100*aid_million_recode, 
                         data = dat,
                         index = c("gid", "year"), model = "within")

gov_dv_b_iv_c.2 <- plm(gov_osv_binary_forward1 ~ pko_n_100 + aid_million_recode + 
                     pko_n_100*aid_million_recode +
                     lnpop + prec_gpcp + conflict_intensity + 
                     spatial_lag_osv + spatial_lag_pko+ 
                     task_tamm + humaid_assist_tamm,
                     data = dat,
                         index = c("gid", "year"), model = "within")


modelsummary(list(dv_c_iv_c.1, dv_c_iv_c.2,
                  reb_dv_c_iv_c.1, reb_dv_c_iv_c.2,
                  gov_dv_c_iv_c.1, gov_dv_c_iv_c.2,
                  dv_b_iv_c.1, dv_b_iv_c.2,
                  reb_dv_b_iv_c.1, reb_dv_b_iv_c.2,
                  gov_dv_b_iv_c.1, gov_dv_b_iv_c.2), stars =  TRUE)

#modelsummary(list(dv_c_iv_c.1, dv_c_iv_c.2,
#                  reb_dv_c_iv_c.1, reb_dv_c_iv_c.2,
#                  gov_dv_c_iv_c.1, gov_dv_c_iv_c.2,
#                  dv_b_iv_c.1, dv_b_iv_c.2,
#                  reb_dv_b_iv_c.1, reb_dv_b_iv_c.2,
#                  gov_dv_b_iv_c.1, gov_dv_b_iv_c.2), stars =  TRUE,
#             output = "-TableA.4.docx")
```

```{r Appendix 5: Models with the nominal variable but only PKO as baseline }
dat <- dat %>%
  mutate(category = case_when(
    treat_aid ==1 & treat_pko == 1 ~ "3",
    treat_aid ==0 & treat_pko == 1 ~ "2",
    treat_aid ==1 & treat_pko == 0 ~ "1",
    treat_aid ==0 & treat_pko == 0 ~ "0"
  ))
class(dat$category)
class(dat)
#Change the baseline to the category 2
dat$category.re <- factor(dat$category, ordered = FALSE)
dat$category.re <- relevel(dat$category.re, ref = "2")

#All sample

apdx_nom.1 <-  plm(log_osv_forward1 ~ category.re  + 
                   lnpop + prec_gpcp  + conflict_intensity + 
                   spatial_lag_osv + spatial_lag_pko+ 
                     task_tamm + humaid_assist_tamm , 
                         data = dat,
                         index = c("gid", "year"), model = "within")

apdx_nom.2 <-  plm(osv_binary_forward1 ~ category.re  + 
                   lnpop +prec_gpcp  + conflict_intensity + 
                   spatial_lag_osv + spatial_lag_pko+ 
                     task_tamm + humaid_assist_tamm , 
                         data = dat,
                         index = c("gid", "year"), model = "within")

#Rebel Sample
reb_apdx_nom.1 <-  plm(log_reb_osv_forward1 ~ category.re  + 
                   lnpop + prec_gpcp  + conflict_intensity + 
                   spatial_lag_osv + spatial_lag_pko+ 
                     task_tamm + humaid_assist_tamm , 
                         data = dat,
                         index = c("gid", "year"), model = "within")

reb_apdx_nom.2 <-  plm(reb_osv_binary_forward1 ~ category.re  + 
                   lnpop +prec_gpcp  + conflict_intensity + 
                   spatial_lag_osv + spatial_lag_pko+ 
                     task_tamm + humaid_assist_tamm , 
                         data = dat,
                         index = c("gid", "year"), model = "within")

#Gov sample
gov_apdx_nom.1 <-  plm(log_gov_osv_forward1 ~ category.re  + 
                   lnpop + prec_gpcp  + conflict_intensity + 
                   spatial_lag_osv + spatial_lag_pko+ 
                     task_tamm + humaid_assist_tamm , 
                         data = dat,
                         index = c("gid", "year"), model = "within")

gov_apdx_nom.2 <-  plm(gov_osv_binary_forward1 ~ category.re  + 
                   lnpop +prec_gpcp  + conflict_intensity + 
                   spatial_lag_osv + spatial_lag_pko+ 
                     task_tamm + humaid_assist_tamm , 
                         data = dat,
                         index = c("gid", "year"), model = "within")

modelsummary(list(apdx_nom.1, apdx_nom.2,
                  reb_apdx_nom.1, reb_apdx_nom.2,
                  gov_apdx_nom.1, gov_apdx_nom.2), stars =  TRUE)
```

